package cn.fatgod.learn.thymeleaf.binder;

import cn.fatgod.learn.thymeleaf.standard.interfaces.Bind;
import cn.fatgod.learn.thymeleaf.standard.interfaces.IDataBinder;
import cn.fatgod.learn.thymeleaf.standard.interfaces.Importable;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Bind
public class AnnotationDataBinder implements IDataBinder, Importable {
    /**
     * 是否开启
     */
    @Bind(false)
    private boolean enable = true;

    /**
     * 注解名称
     */
    private String name;

    /**
     * 注解路径
     */
    private String importPath;

    /**
     * 注解是否有属性
     */
    private boolean hasProperty = false;

    /**
     * 注解属性
     */
    private List<PropertyDataBinder> properties = new ArrayList<>();

    @Override
    public boolean isEnable() {
        return this.enable;
    }

    @Override
    public List<String> getImportPaths() {
        List<String> paths = properties.stream().flatMap(prop -> prop.getImportPaths().stream())
                .distinct()
                .collect(Collectors.toList());
        if (StrUtil.isNotBlank(this.importPath)) {
            paths.add(this.importPath);
        }
        return paths;
    }

    public AnnotationDataBinder setEnable(boolean enable) {
        this.enable = enable;
        return this;
    }

    public String getName() {
        return name;
    }

    public AnnotationDataBinder setName(String name) {
        this.name = name;
        return this;
    }

    public AnnotationDataBinder addProp(PropertyDataBinder prop) {
        if (prop != null) {
            this.hasProperty = true;
        }
        this.properties.add(prop);
        return this;
    }

    public AnnotationDataBinder addProp(List<PropertyDataBinder> props) {
        if (CollUtil.isNotEmpty(props)) {
            this.hasProperty = true;
        }
        this.properties.addAll(props);
        return this;
    }

    public AnnotationDataBinder setImportPath(String importPath) {
        this.importPath = importPath;
        return this;
    }

    public boolean isHasProperty() {
        return this.hasProperty;
    }

    public List<PropertyDataBinder> getProperties() {
        return properties;
    }
}